package com.farsight.leo.fssmartlock.activities;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.farsight.leo.fssmartlock.Constants;
import com.farsight.leo.fssmartlock.R;
import com.farsight.leo.fssmartlock.bases.BaseActivity;
import com.farsight.leo.fssmartlock.bases.BaseApplication;
import com.farsight.leo.fssmartlock.db.DeviceEntity;
import com.farsight.leo.fssmartlock.net.OkHttpHelper;
import com.farsight.leo.fssmartlock.net.bases.BaseOkHttpCallback;
import com.farsight.leo.fssmartlock.net.bean.DeviceData;
import com.farsight.leo.fssmartlock.utils.LogUtils;
import com.farsight.leo.fssmartlock.utils.ToastUtils;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;

public class AddDeviceActivity extends BaseActivity {

    private AppCompatEditText mEdtDeviceName;
    private AppCompatEditText mEdtPw;
    private AppCompatEditText mEdtConfirmPw;
    private AppCompatEditText mEdtBarCode;


    public static void start(Context context) {
        Intent starter = new Intent(context, AddDeviceActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int set_layout_id() {
        return R.layout.activity_add_device;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mEdtBarCode=findViewById(R.id.activity_add_device_edt_bar_code);
        mEdtDeviceName=findViewById(R.id.activity_add_device_edt_input_device_name);
        mEdtConfirmPw=findViewById(R.id.activity_add_device_edt_double_check_pw);
        mEdtPw=findViewById(R.id.activity_add_device_edt_input_pw);

    }

    @Override
    protected void initListeners() {

    }

    public void submit(View view) {
        String deviceName=mEdtDeviceName.getText().toString().trim();
        String pw=mEdtPw.getText().toString().trim();
        String pwConfirm=mEdtConfirmPw.getText().toString().trim();
        String macAddress=mEdtBarCode.getText().toString().trim();
        if(TextUtils.isEmpty(deviceName)||TextUtils.isEmpty(pw)||TextUtils.isEmpty(pwConfirm)||TextUtils.isEmpty(macAddress)){
            return;
        }
        if(!pw.equals(pwConfirm)){
            return;
        }
        DeviceData deviceData=new DeviceData();
        deviceData.setDevice_name(deviceName);
        deviceData.setDevice_password(pw);
        deviceData.setUser_id(BaseApplication.getUserData().getUser_id());
        deviceData.setMac_address(macAddress);

        OkHttpHelper.getInstance().bindDevice(deviceData, new BaseOkHttpCallback<DeviceData>() {
            @Override
            public void onError(String err) {
                ToastUtils.showToast(err);
            }

            @Override
            public void onSuccess(DeviceData result) {
                ToastUtils.showToast("设备添加成功");

                DeviceEntity entry=new DeviceEntity();
                entry.setId(result.getDevice_id());
                entry.setMacAddress(result.getMac_address());
                entry.setUserId(result.getUser_id());
                entry.setUserName(BaseApplication.getUserData().getUser_name());
                BaseApplication.getDeviceDb().getDeviceDao().insertDevice(entry);

                onBackPressed();
            }
        });


    }

    public void beginScanningBarCode(View view) {
        Intent it = new Intent(this, BarCodeScanningActivity.class);
        startActivityForResult(it, Constants.REQUEST_CODE_BAR_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.log("onActivityResult request code=" + requestCode + " resultcode =" + resultCode);
        if (requestCode == Constants.REQUEST_CODE_BAR_CODE && resultCode == Constants.RESULT_CODE_BAR_CODE) {
            String stringExtra = data.getStringExtra(Constants.KEY_BAR_CODE);
            LogUtils.log(stringExtra);
            if (!TextUtils.isEmpty(stringExtra)) {
                mEdtBarCode.setText(stringExtra);
            }
        }
    }
}
